/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.hermeneute.fsa.editors;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.ide.IGotoMarker;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;

import com.hermeneute.fsa.Resources;
import com.hermeneute.fsa.parser.FSTBuilder;


/**
 * An example showing how to create a multi-page editor.
 * This example has 3 pages:
 * <ul>
 * <li>page 0 contains a nested text editor.
 * <li>page 1 allows you to change the font used in page 2
 * <li>page 2 shows the words in page 0 in sorted order
 * </ul>
 */
public class ResultEditor extends MultiPageEditorPart implements IGotoMarker {

	static Button button [] = new Button [4];

	/** The text editor used in page 0. */
	private TextEditor editor;
	/** The index of the page containing the text editor */
	private int editorIndex = 0;
		
	/** The text widget used in page 2. */
	private StyledText text;
	
	Browser browser;
	private Button applyButton = null;
	private Text textResults = null;
	private String textResultsVal = null;
	private String currentProjectPath = null;
	private int currentPage;

	/**
	 * Creates a multi-page editor example.
	 */
	public ResultEditor() {
		super();
	}
	/**
	 * Creates page 0 of the multi-page editor,
	 * which contains a text editor.
	 */
	void createPageEdition() {
		try {
			editor = new TextEditor();
		
			editorIndex = addPage(editor, getEditorInput());
			setPageText(editorIndex, Resources.getString("Edit")); //$NON-NLS-1$
		}
		catch (PartInitException e) {
			ErrorDialog.openError(getSite().getShell(), Resources.getString("ErrorCreatingNestedEditor"), null, e.getStatus()); //$NON-NLS-1$
		}
	}
/**
 * Creates page 1 of the multi-page editor,
 * which allows you to change the font used in page 2.
 */
	void createPageViewer() {
		FormToolkit toolkit = new FormToolkit(getContainer().getDisplay());
		final Form form = toolkit.createForm(getContainer());
		FormLayout layout = new FormLayout();
		form.getBody().setLayout(layout);
		FormData data;
		String doc = ((FileEditorInput)getEditorInput()).getPath().toOSString();
		form.setText("HTML Viewer");
		try {
			browser = new Browser(form.getBody(), SWT.NONE);
		} catch (SWTError x) {
			/* Browser widget could not be instantiated */
			x.printStackTrace();
		}
		
		if(!browser.setUrl("file://"+doc)) {
			System.out.println("Browser failed");
		} else {
			//System.out.println("OK");
		}
		browser.refresh();
		data = new FormData();
		data.top = new FormAttachment(0, 5);
		data.left = new FormAttachment(0, 5);
		data.right = new FormAttachment(100, -5);
		data.bottom = new FormAttachment(100, -5);
		browser.setLayoutData(data);

		data = new FormData();
		data.top = new FormAttachment (0,15);
		data.left = new FormAttachment (0,5);
		data.right = new FormAttachment (100,-5);

		int index = addPage(form);
		setPageText(index, Resources.getString("Viewer")); //$NON-NLS-1$
	}

/**
 * Creates the pages of the multi-page editor.
 */
	protected void createPages() {
		createPageEdition();
		createPageViewer();
		setActivePage(1);
		currentPage = 1;
	}
/**
 * Saves the multi-page editor's document.
 */
	public void doSave(IProgressMonitor monitor) {
		getEditor(0).doSave(monitor);
	}
/**
 * Saves the multi-page editor's document as another file.
 * Also updates the text for page 0's tab, and updates this multi-page editor's input
 * to correspond to the nested editor's.
 */
	public void doSaveAs() {
		IEditorPart editor = getEditor(0);
		editor.doSaveAs();
		setPageText(0, editor.getTitle());
		setInput(editor.getEditorInput());
	}
/**
 * The <code>MultiPageEditorExample</code> implementation of this method
 * checks that the input is an instance of <code>IFileEditorInput</code>.
 */
	public void init(IEditorSite site, IEditorInput editorInput) throws PartInitException {
		if (!(editorInput instanceof IFileEditorInput)) 
			throw new PartInitException(Resources.getString("InvalidInput")); //$NON-NLS-1$
		super.init(site, editorInput);
	}
/* (non-Javadoc)
 * Method declared on IEditorPart.
 */
	public boolean isSaveAsAllowed() {
		return true;
	}
/**
 * Calculates the contents of page 2 when the it is activated.
 */
	protected void pageChange(int newPageIndex) {
		super.pageChange(newPageIndex);
		if (newPageIndex == 2) {
		}
	}
/**
 * Sorts the words in page 0, and shows them in page 2.
 */
	public void build() {
		String filename = ((FileEditorInput)getEditorInput()).getPath().toOSString();
		filename = filename.replaceAll("\\\\","\\\\\\\\");
		try {
			FSTBuilder.fileBuilder(filename,null);
		}catch (Exception x) {
			x.printStackTrace();
		}
		IWorkspace workspace = ResourcesPlugin.getWorkspace(); 
		try {
			workspace.getRoot().refreshLocal(IResource.DEPTH_INFINITE,null);
		}catch(Exception x) {
			x.printStackTrace();
		}
	}

/* (non-Javadoc)
 * @see org.eclipse.ui.ide.IGotoMarker
 */
	public void gotoMarker(IMarker marker) {
		setActivePage(editorIndex);
		IDE.gotoMarker(editor, marker);
	}

}
